home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 34.zip / BS1 part 34 / FredFish PD 304.adf / Lines / clines.c < prev    next >
C/C++ Source or Header  |  1990-01-10  |  9KB  |  408 lines

  1. /*
  2.  * clines.c : Color line pattern generator experimental interface
  3.  *
  4.  * this stuf adapted from mackie.c on fish disk 131.
  5.  *  by Joel Swank 11-13-88
  6.  *
  7.  * Version 1.1 5/26/89
  8.  *
  9.  *
  10.  */
  11.  
  12. #include "window.h"
  13.  
  14. /*
  15.  *   The maximum number of lines on the screen at once.
  16.  */
  17. #define MAXLINES (100)
  18.  
  19. #define INTUITION_REV 1L
  20.  
  21. /*
  22.  *   The global variables.
  23.  */
  24. struct GfxBase    *GfxBase;
  25. struct RastPort *rastport ;
  26. int screenheight, screenwidth;
  27. int erasecolor, curcolor ;
  28. struct Screen *scr, *OpenScreen() ;
  29. struct Window    *Wind = NULL, *OpenWindow() ;
  30. struct MsgPort    *CreatePort() ;
  31. struct IntuitionBase    *IntuitionBase ;
  32. struct IntuiMessage    *msg, *GetMsg();
  33. struct Library *OpenLibrary();
  34. struct TextFont *OpenFont();
  35. struct Window *wH = NULL;        /* help window */
  36. struct RastPort *rpH = NULL;     /* help RastPort */
  37. struct TextFont *font = NULL;
  38.  
  39. USHORT keycode, stop, rotcolor, bkgnd, freeze, erase;
  40. ULONG class;
  41.  
  42. /*
  43.  *   MainLine
  44.  */
  45.  
  46. main() {
  47.    long i ;
  48.  
  49.    screenheight = YSIZE;
  50.    screenwidth = XSIZE;
  51.  
  52.    if ((IntuitionBase = (struct IntuitionBase *)
  53.        OpenLibrary("intuition.library", INTUITION_REV)) == NULL)
  54.       done(21);
  55.  
  56.    GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", NULL);
  57.    if (GfxBase == NULL) done(22);
  58.  
  59.    if ( (scr = OpenScreen(&newscreen)) == NULL)  
  60.        {
  61.     AutoRequest(0L,&scrfailtxt,0L,&oktxt,0L,0L,300L,75L);
  62.     done(26);
  63.     }
  64.    New_Window.Screen = scr;
  65.  
  66.    if ((Wind = (struct Window *) OpenWindow(&New_Window)) == NULL)
  67.       done(25);
  68.    rastport = Wind->RPort;
  69.  
  70.    LoadRGB4(&(scr->ViewPort),&Palette,PaletteColorCount);
  71.  
  72.    clr_grf();
  73.    do_help();
  74.  
  75.    startlines() ;
  76.    for (i=0; i<MAXLINES; i++) {
  77.       advancelines() ;
  78.       drawnew() ;
  79.    }
  80.    colors() ;
  81.    stop = FALSE;
  82.    freeze = FALSE;
  83.    erase = TRUE;
  84.    rotcolor = TRUE;
  85.  
  86.    while (1) {
  87.  
  88.     while((msg = (struct IntuiMessage *) GetMsg(Wind->UserPort)) ||
  89.          (msg = (struct IntuiMessage *) GetMsg(wH->UserPort))) {
  90.         keycode = msg->Code;
  91.         class = msg->Class;
  92.         ReplyMsg(msg);
  93.         switch(class) {
  94.             case VANILLAKEY:
  95.                 switch (keycode)
  96.                     {
  97.                     case ' ':        /* start/stop */
  98.                       if (stop == TRUE) stop = FALSE;
  99.                       else stop = TRUE;
  100.                       break;
  101.                     case 'o':        /* one iteration */
  102.                       stop = TRUE;
  103.                       goto once;
  104.                     case 'e':        /* toggle erase */
  105.                       if (erase == TRUE) erase = FALSE;
  106.                       else erase = TRUE;
  107.                       break;
  108.                     case 'r':        /* toggle pallete changes */
  109.                       if (rotcolor == TRUE)  rotcolor = FALSE;
  110.                       else rotcolor = TRUE;
  111.                       break;
  112.                     case 'b':        /* Black/white background */
  113.                       if (bkgnd)
  114.                           {
  115.                         bkgnd = FALSE;
  116.                           SetRGB4(&(scr->ViewPort), 0L, 0L, 0L, 0L);
  117.                       } else {
  118.                           SetRGB4(&(scr->ViewPort), 0L, 15L, 15L, 15L);
  119.                         bkgnd = TRUE;
  120.                         }
  121.                       break;
  122.                     case 'f':        /* freeze/thaw color changing */
  123.                       if (freeze == TRUE) freeze = FALSE;
  124.                       else freeze = TRUE;
  125.                       break;
  126.                     case 'a':        /* do about requester  */
  127.                       about();
  128.                       break;
  129.                     case 'q':        /* Exit */
  130.                     case 'x':
  131.                     case '\033':
  132.                       done(0);
  133.                       break;
  134.                     }
  135.                 continue;
  136.             case REFRESHWINDOW :
  137.                 draw_help();
  138.                 break;
  139.             case CLOSEWINDOW:
  140.                 done(0);
  141.             }
  142.         }
  143.       if (stop == TRUE) 
  144.           {
  145.         Wait(( 1L << Wind->UserPort->mp_SigBit) |    /* wait on mesg */
  146.             ( 1L << wH->UserPort->mp_SigBit));
  147.         continue;
  148.         }
  149.  
  150.    once:
  151.       if (erase) eraseold() ;
  152.       advancelines() ;
  153.       drawnew() ;
  154.       if (erase) eraseold() ;
  155.       advancelines() ;
  156.       drawnew() ;
  157.       if (erase) eraseold() ;
  158.       advancelines() ;
  159.       drawnew() ;
  160.       if (erase) eraseold() ;
  161.       advancelines() ;
  162.       drawnew() ;
  163.       if (erase) eraseold() ;
  164.       advancelines() ;
  165.       drawnew() ;
  166.       if (erase) eraseold() ;
  167.       advancelines() ;
  168.       drawnew() ;
  169.       colors() ;
  170.    }
  171. }
  172. /*
  173.  * End of MainLine 
  174.  */
  175.  
  176.  
  177. /*
  178.  * done - just clean up that which is open, and then leave.
  179.  */
  180. done(how)
  181. int how;
  182.     {
  183.     if (wH) CloseWindow(wH) ;
  184.     if (Wind) CloseWindow(Wind) ;
  185.     if (scr) CloseScreen(scr) ;
  186.     if (font) CloseFont(font) ;
  187.     if (GfxBase) CloseLibrary(GfxBase) ;
  188.     if (IntuitionBase) CloseLibrary(IntuitionBase) ;
  189.  
  190.     exit(how) ;
  191.     }
  192.  
  193. /*
  194.  *   This routine returns a random value from 0 to n-1.
  195.  */
  196. int randm(i)
  197. int i ;
  198. {
  199.    static long seed ;
  200.    long rval ;
  201.  
  202.    if (seed == 0)
  203.       seed = 323214521 + scr->MouseX +
  204.               scr->MouseY ;
  205.    seed = seed * 123213 + 121 ;
  206.    rval = (seed >> 5) & 65535 ;
  207.    return ((i * rval) >> 16) ;
  208. }
  209. /*
  210.  *   This routine sets x and y values to a random number.
  211.  */
  212. static long x, y ;
  213. randomxy() {
  214.    x = randm(screenwidth) ;
  215.    y = randm(screenheight) ;
  216. }
  217. /*
  218.  *   Main routines are always fun.
  219.  */
  220. short x1store[MAXLINES], y1store[MAXLINES] ;
  221. short x2store[MAXLINES], y2store[MAXLINES] ;
  222. short ptr ;
  223. short dx1, dy1, dx2, dy2 ;
  224. short ox1, oy1, ox2, oy2 ;
  225. short nx1, ny1, nx2, ny2 ;
  226. short dr[8], dg[8], db[8] ;
  227. short or, og, ob ;
  228. short nr[8], ng[8], nb[8] ;
  229. /*
  230.  *   Initialize things for the first lines.
  231.  */
  232. startlines() {
  233.    int i;
  234.  
  235.    ptr = 0 ;
  236.    ox1 = randm(screenwidth) ;
  237.    ox2 = randm(screenwidth) ;
  238.    oy1 = randm(screenheight) ;
  239.    oy2 = randm(screenheight) ;
  240.    dx1 = 3 ;
  241.    dx2 = 4 ;
  242.    dy1 = 1 ;
  243.    dy2 = 6 ;
  244.    for (i=1;i < 8; i++)
  245.       {
  246.       nr[i] = (Palette[i] & 0x0F00) >> 5 ;
  247.       ng[i] = (Palette[i] & 0x00F0) >> 1 ;
  248.       nb[i] = (Palette[i] & 0x000F) << 3 ;
  249.       dr[i] = -3 ;
  250.       dg[i] = 5 ;
  251.       db[i] = 7 ;
  252.       }
  253. }
  254. /*
  255.  *   Advance the number by the delta, and check the boundaries.
  256.  */
  257. adv(o, d, n, w)
  258. short *o, *d, *n ;
  259. long w ;
  260. {
  261.    *n = *o + *d ;
  262.    if (*n < 0) {
  263.       *n = 0 ;
  264.       *d = randm(6) + 1 ;
  265.    } else if (*n >= w) {
  266.       *n = w - 1 ;
  267.       *d = - randm(6) - 1 ;
  268.    }
  269. }
  270. /*
  271.  *   Advance the two points which make up the lines.
  272.  */
  273. advancelines() {
  274.    adv(&ox1, &dx1, &nx1, screenwidth) ;
  275.    adv(&ox2, &dx2, &nx2, screenwidth) ;
  276.    adv(&oy1, &dy1, &ny1, screenheight) ;
  277.    adv(&oy2, &dy2, &ny2, screenheight) ;
  278. }
  279. /*
  280.  *   Draw a new set of lines.
  281.  */
  282. drawnew() {
  283.    x1store[ptr] = ox1 = nx1 ;
  284.    x2store[ptr] = ox2 = nx2 ;
  285.    y1store[ptr] = oy1 = ny1 ;
  286.    y2store[ptr] = oy2 = ny2 ;
  287.    Move(rastport, (long)ox1, (long)oy1) ;
  288.    Draw(rastport, (long)ox2, (long)oy2) ;
  289.    Draw(rastport, (long)(screenwidth-ox1-1), (long)(screenheight-oy1-1)) ;
  290.    Draw(rastport, (long)(screenwidth-ox2-1), (long)(screenheight-oy2-1)) ;
  291.    Draw(rastport, (long)ox1, (long)oy1) ;
  292.    ptr++ ;
  293.    if (ptr == MAXLINES)
  294.       ptr = 0 ;
  295. }
  296. /*
  297.  *   Erase the old line.
  298.  */
  299. eraseold() {
  300.    short oldpen ;
  301.  
  302.    oldpen = rastport->FgPen ;
  303.    SetAPen(rastport, 0L) ;
  304.    Move(rastport, (long)x1store[ptr], (long)y1store[ptr]) ;
  305.    Draw(rastport, (long)x2store[ptr], (long)y2store[ptr]) ;
  306.    Draw(rastport, (long)(screenwidth-x1store[ptr]-1),
  307.                   (long)(screenheight-y1store[ptr]-1)) ;
  308.    Draw(rastport, (long)(screenwidth-x2store[ptr]-1), 
  309.                   (long)(screenheight-y2store[ptr]-1)) ;
  310.    Draw(rastport, (long)x1store[ptr], (long)y1store[ptr]) ;
  311.    SetAPen(rastport, (long)oldpen) ;
  312. }
  313. /*
  314.  *   This routine mucks with the colors.
  315.  */
  316. colors() {
  317.  
  318.    if (!freeze)
  319.       {
  320.       if (++curcolor > 7) curcolor = 1 ;
  321.       SetAPen(rastport, (long)curcolor) ;
  322.       }
  323.  
  324.    if (rotcolor && curcolor != 0)
  325.       {
  326.       or = nr[curcolor] ;
  327.       og = ng[curcolor] ;
  328.       ob = nb[curcolor] ;
  329.       adv(&or, &dr[curcolor], &nr[curcolor], 128) ;
  330.       adv(&og, &dg[curcolor], &ng[curcolor], 128) ;
  331.       adv(&ob, &db[curcolor], &nb[curcolor], 128) ;
  332.       SetRGB4(&(scr->ViewPort), (long)curcolor, (long)(nr[curcolor] >> 3),
  333.                 (long)(ng[curcolor] >> 3), (long)(nb[curcolor] >> 3)) ;
  334.       }
  335.  
  336. }
  337.  
  338.  
  339.  
  340.  
  341. /*
  342.  * do_help - display help text
  343.  */
  344.  
  345. do_help()
  346. {
  347.  
  348.     NewWindowStructureHelp.Screen = scr;
  349.  
  350.     wH = OpenWindow(&NewWindowStructureHelp);    /* open the window */
  351.     if ( wH == NULL )
  352.         {
  353.         AutoRequest(Wind,&winfailtxt,0L,&oktxt,0L,0L,300L,75L);
  354.         done(20);
  355.         }
  356.  
  357.     rpH = wH->RPort;    /* get a rastport pointer for the window */
  358.  
  359.     font = OpenFont(&TOPAZ80); /* make sure correct size font */
  360.     if (font) SetFont(rpH,font);
  361.     SetAPen(rpH,1L);
  362.     draw_help();
  363. }
  364.  
  365. draw_help()
  366. {
  367.     int i;
  368.     for (i=0; i<MAXHELP; i++)    /* dump the whole help text array */
  369.         {
  370.         if (!HelpText[i]) break;
  371.         Move(rpH,5L,(long) (i+1)*9+20);
  372.         Text(rpH,HelpText[i], (long) strlen(HelpText[i]));
  373.         }
  374. }
  375.  
  376.  
  377.  
  378. /*
  379.  *  Handle 'about' request
  380.  */
  381.  
  382.  
  383. about()
  384. {
  385.     AutoRequest(wH,&aboutmsg,0L,&oktxt,0L,0L,300L,75L);
  386. }
  387.  
  388.  
  389. clr_grf()
  390. {
  391.    SetAPen(rastport, 0L) ;
  392.    Forbid() ;
  393.    RectFill(rastport, 0L, 0L, (long)screenwidth-1, (long)screenheight-1) ;
  394.    Permit() ;
  395.    SetAPen(rastport, 1L) ;
  396. }
  397.  
  398. #ifdef AZTEC_C
  399. _wb_parse()
  400. {
  401. }
  402.  
  403. _abort()
  404. {
  405.     done(13);
  406. }
  407. #endif
  408.